package xyz.zhno.mall.product.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.zhno.mall.common.mpg.entity.PmsBrand;
import xyz.zhno.mall.common.mpg.query.BaseQuery;
import xyz.zhno.mall.common.mpg.vo.BasePage;
import xyz.zhno.mall.product.model.param.PmsBrandParam;
import xyz.zhno.mall.product.web.service.PmsBrandService;

import javax.validation.constraints.NotNull;

/**
 * 品牌表现层
 * @author zh_o
 */
@RestController
@AllArgsConstructor
@RequestMapping("/brand")
public class PmsBrandController {

    private final PmsBrandService brandService;

    /**
     * 分页查询品牌列表
     * @param query 查询参数
     * @return 品牌列表
     */
    @GetMapping("/list")
    public BasePage<PmsBrand> list(@Validated BaseQuery query) {
        return BasePage.newBasePage(
                brandService.page(new Page<>(query.getPageNum(), query.getPageSize()),
                        new LambdaQueryWrapper<PmsBrand>().orderByDesc(PmsBrand::getSort))
        );
    }

    /**
     * 添加品牌
     * @param param 参数列表
     * @return 添加品牌的ID
     */
    @PostMapping("/add")
    public Long create(@Validated PmsBrandParam param) {
        return brandService.create(param);
    }

    /**
     * 修改品牌
     * @param id 需要修改品牌ID
     * @param param 参数列表
     * @return 修改品牌的ID
     */
    @PutMapping("/update")
    public Long create(@Validated @NotNull Long id, @Validated PmsBrandParam param) {
        return brandService.update(id, param);
    }

    /**
     * 删除品牌
     * @param id 需要删除品牌ID
     * @return 删除品牌的ID
     */
    @DeleteMapping("/delete")
    public Long delete(@Validated @NotNull Long id) {
        brandService.removeById(id);
        return id;
    }

}
